package net.expectx.gw.controller;

import com.alibaba.fastjson.JSONObject;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import net.expectx.common.base.BaseResult;
import net.expectx.gw.bean.SystemSetting;
import net.expectx.gw.bean.SystemType;
import net.expectx.gw.bean.SystemTypeGroup;
import net.expectx.gw.service.SystemSettingService;
import net.expectx.gw.service.SystemTypeGroupService;
import net.expectx.gw.util.AdminConstants;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.*;

/**
 * controller
 * @author lijian
 * @date 2019/1/18
 */
@Controller
@RequestMapping("/manager/system/systemSetting")
@Api(value = "控制器", description = "管理")
public class SystemSettingController extends BaseController {

    private static final Logger LOGGER = LoggerFactory.getLogger(SystemSettingController.class);


    @Autowired
    SystemSettingService systemSettingService;
    @Autowired
    SystemTypeGroupService systemTypeGroupService;
    @ApiOperation(value = "系统设置主页")
    @RequestMapping(value = "/index", method = RequestMethod.GET)
    public String index(HttpServletRequest request) {
        List<SystemSetting> systemSettings=systemSettingService.findByAttr("is_deleted=0",null);
        SystemTypeGroup systemTypeGroup=systemTypeGroupService.findByAttr(" is_deleted=0 and type_group_code in ('web_site')",null).get(0);
        List<SystemType> systemTypes= systemTypeGroup.getSystemTypes();
        List<SystemType> types=systemTypeGroupService.findByAttr(" is_deleted=0 and type_group_code in ('form_type')",null).get(0).getSystemTypes();
        request.setAttribute("fromType",types);
        request.setAttribute("systemSetting",systemSettings);
        request.setAttribute("systemTypes",systemTypes);
        request.setAttribute("webSite",systemTypes);

        return "setting/index";
    }
    @ApiOperation(value = "添加设置")
    @RequestMapping(value = "/create", method = RequestMethod.POST)
    @ResponseBody
    public Object create(SystemSetting systemSetting,HttpServletRequest request) {
        systemSetting.setGmtCreate(new Date());
        systemSetting.setGmtModified(new Date());
        systemSetting.setIsDeleted(AdminConstants.NO);
        systemSetting.setOrders(Long.parseLong("0"));
        request.getSession().setAttribute("attrClass",systemSetting.getAttrClass());
        request.getSession().setAttribute("num","0");
        return systemSettingService.insertSelective(systemSetting);
    }
    @ApiOperation(value = "添加系统值")
    @RequestMapping(value = "/createValue", method = RequestMethod.POST)
    @ResponseBody
    public Object createValue(String systemSettingValue) {
        BaseResult resultMap = new BaseResult(0, "", null);
        JSONObject json = JSONObject.parseObject(systemSettingValue);
        Set keySet=json.keySet();
        json.remove("file");
        for(Object key:keySet){
            String attrValue=json.getString(key.toString());
            if(key.toString().contains("setting")){
                key=key.toString().substring(7);
            }
            System.out.println(key);
            Long id=Long.parseLong(key.toString());
            SystemSetting systemSetting=systemSettingService.selectByPrimaryKey(id);
            systemSetting.setAttrValue(attrValue);
            resultMap=systemSettingService.updateByPrimaryKeySelective(systemSetting);


        }

        return resultMap;
    }
    @ApiOperation(value = "删除系统")
    @RequestMapping(value = "/delete", method = RequestMethod.GET)
    @ResponseBody
    public Object delete(String id) {
        Map<String, Object> map = new HashMap<>(2);
        int count = systemSettingService.logicDeletion(AdminConstants.YES, "id in(" + id + ")");
        if (count > 0) {
            count = systemSettingService.logicDeletion(AdminConstants.YES, "id in(" + id + ")");
            map.put("flag", count > 0 ? true : false);
        } else {
            map.put("flag", count > 0 ? true : false);

        }
        return map;
    }
    @ApiOperation(value = "修改系统设置")
    @RequestMapping(value = "/update/{id}", method = RequestMethod.GET)
    @ResponseBody
    public Object update(@PathVariable("id") Long id, HttpServletRequest request) {
        Map<String, Object> resultData=new HashMap<>(2);
        SystemSetting systemSetting = systemSettingService.selectByPrimaryKey(id);
        request.getSession().setAttribute("system", systemSetting);
        request.getSession().setAttribute("num","0");
        request.getSession().setAttribute("attrClass",systemSetting.getAttrClass());
        resultData.put("flag",true);
        return resultData;
    }
    @ApiOperation(value = "修改系统设置")
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    @ResponseBody
    public Object update(SystemSetting systemSetting) {
        systemSetting.setGmtModified(new Date());
        return systemSettingService.updateByPrimaryKeySelective(systemSetting);
    }
    @ApiOperation(value = "上传图片")
    @RequestMapping(value = "/updateImg/{id}", method = RequestMethod.GET)
    public String update(@PathVariable("id") Long id, ModelMap modelMap) {
        SystemSetting systemSetting = systemSettingService.selectByPrimaryKey(id);
        modelMap.put("systemSetting", systemSetting);
        return "system/systemSetting/modify";
    }
}
